﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using TrainningCShape_Week01.BL;
using TrainningCShape_Week01.DO;
using TrainningCShape_Week01.GUI;

namespace TrainningCShape_Week01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// xử lý đóng chương trình khi ấn nút cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btncancel_Click(object sender, EventArgs e)
        {
            //DialogResult result = MessageBox.Show("Ban muon thoat?", "Canh bao", MessageBoxButtons.YesNo);
            //if (result == DialogResult.Yes)
            //{
            //    this.Close();
            //}
            this.Close();
        }

        /// <summary>
        /// sử lý chọn tất cả khi chon vao textbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Enter(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            txt.SelectAll();
        }

        /// <summary>
        /// check username
        /// </summary>
        /// <returns></returns>
        private bool CheckUsername() 
        {
            Match m = Regex.Match(txtusername.Text, "(^[A-Za-z0-9_.]{6,}$)");
            if (m.Success)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Check password
        /// </summary>
        /// <returns></returns>
        private bool CheckPassword()
        {
            return Regex.IsMatch(txtpassword.Text, "(^[A-Za-z0-9_~!@#$%^&*()-=_+:;<>?/`”’\\s]{4,}$)");
        }

        /// <summary>
        /// kiem tra username va password khi nhap submit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsubmit_Click(object sender, EventArgs e)
        {
            //if (CheckUsername() && CheckPassword())
            //{
            //    MessageBox.Show("Good Job");
            //}
            //else
            //{
            //    MessageBox.Show("Ten Dang Nhap Hoac Mat Khau Khong Hop Le");
            //}

            if (CheckUsername() && CheckPassword())
            {
                UserInfo user = new UserInfo(txtusername.Text, txtpassword.Text);
                //user.Username = txtusername.Text;
                //user.Password = txtpassword.Text;
                if (LoginControl.validateUser(user))
                {
                    MessageBox.Show("DANG NHAP THANH CONG");

                }
                else
                {
                    MessageBox.Show("TEN DANG NHAP VA MAT KHAU KHONG HOP LE");
                }
            }
            else
            {
                MessageBox.Show("TEN DANG NHAP VA MAT KHAU KHONG HOP LE");
            }
           


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("Ban muon thoat?", "Canh bao", MessageBoxButtons.YesNo);
            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        
        // mo bang moi
        private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.ShowDialog();
            
        }

        
      
    }
}
